﻿using System; 
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;
using RBS.DBUtility;
using RBS.Common;
namespace RBS.Core.DAL  
{
	 	//广告券批次表
		public partial class P_Advertis_Batch
	{
   		     
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from P_Advertis_Batch");
			strSql.Append(" where ");
			                                       strSql.Append(" Id = @Id  ");
                            			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}
		
				
		
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(RBS.Core.Model.P_Advertis_Batch model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into P_Advertis_Batch(");			
            strSql.Append("AP_Id,BatchNo,BatchTime,ABStatus");
			strSql.Append(") values (");
            strSql.Append("@AP_Id,@BatchNo,@BatchTime,@ABStatus");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");		
			SqlParameter[] parameters = {
			            new SqlParameter("@AP_Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@BatchNo", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@BatchTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@ABStatus", SqlDbType.Int,4)             
              
            };
			            
            parameters[0].Value = model.AP_Id;                        
            parameters[1].Value = model.BatchNo;                        
            parameters[2].Value = model.BatchTime;                        
            parameters[3].Value = model.ABStatus;                        
			   
			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);			
			if (obj == null)
			{
				return 0;
			}
			else
			{
				                    
            	return Convert.ToInt32(obj);
                                                                  
			}			   
            			
		}
		
		
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(RBS.Core.Model.P_Advertis_Batch model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update P_Advertis_Batch set ");
			                                                
            strSql.Append(" AP_Id = @AP_Id , ");                                    
            strSql.Append(" BatchNo = @BatchNo , ");                                    
            strSql.Append(" BatchTime = @BatchTime , ");                                    
            strSql.Append(" ABStatus = @ABStatus  ");            			
			strSql.Append(" where Id=@Id ");
						
SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@AP_Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@BatchNo", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@BatchTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@ABStatus", SqlDbType.Int,4)             
              
            };
						            
            parameters[0].Value = model.Id;                        
            parameters[1].Value = model.AP_Id;                        
            parameters[2].Value = model.BatchNo;                        
            parameters[3].Value = model.BatchTime;                        
            parameters[4].Value = model.ABStatus;                        
            int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from P_Advertis_Batch ");
			strSql.Append(" where Id=@Id");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;


			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from P_Advertis_Batch ");
			strSql.Append(" where ID in ("+Idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
				
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public RBS.Core.Model.P_Advertis_Batch GetModel(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Id, AP_Id, BatchNo, BatchTime, ABStatus  ");			
			strSql.Append("  from P_Advertis_Batch ");
			strSql.Append(" where Id=@Id");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			
			RBS.Core.Model.P_Advertis_Batch model=new RBS.Core.Model.P_Advertis_Batch();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			
			if(ds.Tables[0].Rows.Count>0)
			{
												if(ds.Tables[0].Rows[0]["Id"].ToString()!="")
				{
					model.Id=int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["AP_Id"].ToString()!="")
				{
					model.AP_Id=int.Parse(ds.Tables[0].Rows[0]["AP_Id"].ToString());
				}
																																				model.BatchNo= ds.Tables[0].Rows[0]["BatchNo"].ToString();
																												if(ds.Tables[0].Rows[0]["BatchTime"].ToString()!="")
				{
					model.BatchTime=DateTime.Parse(ds.Tables[0].Rows[0]["BatchTime"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["ABStatus"].ToString()!="")
				{
					model.ABStatus=int.Parse(ds.Tables[0].Rows[0]["ABStatus"].ToString());
				}
																														
				return model;
			}
			else
			{
				return null;
			}
		}
		
		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM P_Advertis_Batch ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" * ");
			strSql.Append(" FROM P_Advertis_Batch ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		
		 #region  ExtensionMethod
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM V_BatchList ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where 1=1 " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
		#endregion  ExtensionMethod


        
        /// <summary>
        /// 获取剩余优惠券
        /// </summary>
        /// <param name="apid"></param>
        /// <returns></returns>
        public int GetLave(int apid)
        {
            const string strSql = @" select p.Id, p.Quantity-isnull(count(b.AP_Id),0)  as num  from P_Advertis_Payment p 
                left join P_Advertis_Batch b  on  p.Id=b.AP_Id where p.Id=@apid and p.DeedStatus=0 group by p.Id,p.Quantity ";

            SqlParameter[] parameters = {
					new SqlParameter("@apid", SqlDbType.Int,4)
			};
            parameters[0].Value = apid;
             DataSet ds= DbHelperSQL.Query(strSql,parameters);
             if (ds.Tables[0].Rows.Count > 0)
             {
                 return Convert.ToInt32(ds.Tables[0].Rows[0]["num"]);
             }
            return 0;
        }
    }
}

